package com.hjy;

import java.util.Stack;
//判断传入的字符串 {{([ 等括号是否乘次匹配
public class Solution {
    public boolean isValid(String s){
        Stack<Character> sta = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if("(".equals(c) || "[".equals(c) || "{".equals(c)){
                sta.push(c);
            }else{
                if(sta.isEmpty()) return false;
                Character toChar = sta.pop();
                if (")".equals(c) && !toChar.equals("(")){
                    return false;
                }
                if ("]".equals(c) && !toChar.equals("[")){
                    return false;
                }
                if ("}".equals(c) && !toChar.equals("{")){
                    return false;
                }
            }
        }

        return sta.isEmpty()    ;
    }
}
